<?php

namespace App\Traits\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

Trait DestroyTraits
{

    /**
     * 删除
     *
     * @param $id
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function destroy($id, Request $request)
    {
        //explode() 函数把字符串打散为数组。
        //array_filter() 函数用回调函数过滤数组中的元素。删除数组中 array 的所有“空”元素
        $ids = array_filter(explode(',', $id));

        if (empty($ids)) {
            return $this->response->errorBadRequest();
        }
        $this->getQuery()->whereIn($this->getQuery()->getKeyName(), $ids)->delete();

        return $this->response->noContent();
    }

}
